<template>
    <div class="cl-pagination">
        <div class="arrow" @click="prev">
            <i class="el-icon-arrow-left"></i>
        </div>
        <div class="arrow" @click="next">
            <i class="el-icon-arrow-right"></i>
        </div>

        <div class="pages">
            <span>{{ current }}</span>/{{ totalPage }}
        </div>

        <div class="page-jumper">
            <span>到第</span>
            <el-input
              class="el-pagination__editor is-in-pagination"
              v-model="input"
              :min="1"
              :max="totalPage"
              type="number"
              @change="handleChange"></el-input>

            <span>页</span>
            <!--<cl-button customize="submit">确定</cl-button>-->
        </div>

        <img class="setting" @click="goSetting" src="../assets/image/setting.png">

        <div class="setting-wrapper" v-show="showSetting" @click="prevent">
            <span class="hint">每页显示</span>
            <cl-button
              v-for="(n, i) in settings"
              :key="'pagination-setting-' + i"
              :pattern="size === n ? 'primary' : 'default'"
              customize="pagination-button"
              @click="handleSizeChange(n)">{{ n }}
            </cl-button>

            <el-input
              class="el-pagination__editor is-in-pagination"
              v-model="sizeInput"
              type="number"
              @change="handleSizeChange"
              placeholder="自定义"></el-input>
        </div>
    </div>
</template>

<script>
import { on, off } from 'element-ui/src/utils/dom'

export default {
    name: 'ClPagination',
    props: {
        page: Number,
        pageSize: Number,
        total: Number
    },
    data() {
        return {
            settings: [20, 50, 100],
            current: 1,
            size: 20,
            totalPage: 1,
            input: 1,
            sizeInput: '',
            showSetting: false
        }
    },
    mounted() {
        this.current = this.page
        this.size = this.pageSize
        this.handleTotal()
    },
    methods: {
        prevent(e) {
            e.preventDefault()
            e.stopPropagation()
        },
        handleTotal() {
            if (this.total) {
                let count = parseInt(this.total / this.pageSize)
                this.totalPage = (this.total % this.pageSize === 0) ? count : count + 1
            }
        },
        handleChange(v) {
            v = parseInt(v)
            if (v > this.totalPage) v = this.totalPage
            if (v < 1) v = 1

            this.input = v
            this.current = v
            this.$emit('update:page', this.current)
        },
        handleSizeChange(v) {
            v = parseInt(v)
            if (v < 1) v = 1

            this.size = v
            this.$emit('update:pageSize', this.size)

            this.current = 1
            this.$emit('update:page', this.current)

            this.handleTotal()
        },
        prev() {
            this.handleChange(this.current - 1)
        },
        next() {
            this.handleChange(this.current + 1)
        },
        goSetting(e) {
            this.showSetting = !this.showSetting
            if (this.showSetting) {
                setTimeout(() => {
                    on(document, 'click', this.handleDocumentClick)
                }, 100)
            }

            e.preventDefault()
            e.stopPropagation()
        },
        handleDocumentClick() {
            this.showSetting = false
            off(document, 'click', this.handleDocumentClick)
        }
    },
    watch: {
        size() {
            let index = this.settings.indexOf(this.size)
            if (index < 0) this.sizeInput = this.size
            else this.sizeInput = ''
        },
        page() {
            this.handleChange(this.page)
        },
        pageSize() {
            this.handleSizeChange(this.pageSize)
        },
        total() {
            this.handleTotal()
        }
    }
}
</script>

<style lang="less">
    @import "../assets/css/variable/variable.less";

    .el-pagination__editor {
        height: 26px;
    }

    .el-pagination__editor.el-input {
        width: 46px;
    }

    .el-pagination__editor .el-input__inner {
        padding: 0;
        text-align: center;
        font-size: 10px;

        border: 1px solid @frameColor;
        border-radius: 2px;
        box-sizing: border-box;
    }

    .el-pagination__editor.el-input .el-input__inner {
        height: 26px;
    }

    .el-input.is-active .el-input__inner, .el-input__inner:focus {
        border-color: @primaryColor;
    }
</style>

<style lang="less" scoped>
    @import "../assets/css/variable/variable.less";

    .cl-button {
        margin-left: 4px;
    }

    .cl-button + .cl-button {
        margin-left: 4px;
    }

    .border {
        border: 1px solid @frameColor;
        border-radius: 2px;
        box-sizing: border-box;
    }

    div {
        display: flex;
        align-items: center;
    }

    .cl-pagination {
        position: relative;

        .arrow {
            justify-content: center;

            width: 26px;
            height: 26px;

            font-size: 12px;
            font-weight: bold;
            color: #666;
            cursor: pointer;

            .border();

            &:hover {
                border: 1px solid @primaryColor;
                color: @primaryColor;
            }
        }

        .arrow + .arrow {
            margin-left: 6px;
        }

        .pages {
            font-size: 10px;
            color: @contentColor;
            margin: 0 6px;

            span {
                color: @primaryColor;
            }
        }

        .page-jumper {
            font-size: 10px;
            color: @contentColor;
            margin-right: 12px;

            .jumper-input {
                width: 46px;
            }
        }

        .pagination-button {
            font-size: 10px;
            width: 26px;
            border-radius: 2px;
            padding: 2px 0;
        }

        .setting {
            width: 12px;
            height: 15px;
            cursor: pointer;
        }

        .setting-wrapper {
            position: absolute;
            bottom: 40px;
            right: 0;

            background: white;
            padding: 16px 22px;

            font-size: 10px;
            color: @contentColor;

            border-radius: 5px 5px 0 5px;
            box-shadow: 0px -2px 6px 0px rgba(92, 100, 113, 0.11);

            z-index: 100;

            .hint {
                word-break: keep-all;
            }
        }
    }
</style>
